package de.urwalking.legobaukasten;

import java.util.ArrayList;

import android.content.Intent;
import android.hardware.SensorEvent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import de.rj.urwalking.R;
import de.rj.urwalking.config.Config;
import de.rj.urwalking.config.Constants;
import de.rj.urwalking.data.Perspective;
import de.rj.urwalking.data.Poi;
import de.rj.urwalking.ui.activities.URwalkingActivity;
import de.rj.urwalking.util.ExperimentManager;
import de.rj.urwalking.util.FileUpload;
import de.rj.urwalking.util.PrefUtil;

public class LegoActivity extends URwalkingActivity {

	/** the fragment which contains all the 3d stuff */
	private LegoFragment mFragment;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lego);

		initUI();
		initSensors();

		// start Upload task
		if (!FileUpload.getInstance().isFileUploadTaskRunning()) {
			Log.i(Config.UPLOADTAG, "Start upload task onCreate of LegoActivity.");
			FileUpload.getInstance().start();
		}
		
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.lego_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.urwalking_menu_switch_walls:
			mFragment.setWallVisibility();
			break;
		case R.id.urwalking_menu_change_level:
			mFragment.changeLevel();
			break;
		case R.id.urwalking_menu_change_perspective:
			mFragment.changePerspective();
			break;
		case R.id.urwalking_menu_poi:
			mFragment.newLandmark();
			break;
		case R.id.menu_compass:
			toggleCompass();
			ActivityCompat.invalidateOptionsMenu(this);
			break;
		case R.id.urwalking_menu_gps:
			toggleGPS();
			break;
		case R.id.urwalking_menu_settings:
			//startActivityForResult(new Intent(this, OutdoorPreferenceActivity.class),
			//		Constants.PREFERENCE_ACTIVITY_REQUEST_CODE);

			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		// set the state of the experiment control menu item
		MenuItem item;


		// set the state of the change perspective menu item
		item = menu.findItem(R.id.urwalking_menu_change_perspective);

		if (!allowViewChange) {
			item.setVisible(false);
			item.setEnabled(false);
		} else if (mFragment.getPerspective() == Perspective.TYPE_3D_ABOVE) {
			item.setTitle(R.string.to_streetview);
		} else if (mFragment.getPerspective() == Perspective.TYPE_3D_STREET) {
			item.setTitle(R.string.to_overview);
		}

		// set the state of the compass menu item
		item = menu.findItem(R.id.menu_compass);

		if (useCompass && mCompassIsAccurate) {
			item.setIcon(R.drawable.ic_menu_compass_active);
		} else if (useCompass && !mCompassIsAccurate) {
			item.setIcon(R.drawable.ic_menu_compass_inaccurate);
		} else {
			item.setIcon(R.drawable.ic_menu_compass);
		}

		// set the state of the gps menu item
		item = menu.findItem(R.id.urwalking_menu_gps);

		if (followGPS) {
			item.setIcon(R.drawable.ic_menu_mylocation_active);
		} else {
			item.setIcon(R.drawable.ic_menu_mylocation);
		}

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (PrefUtil.inTestMode(this) && !PrefUtil.readBoolean(R.string.key_pref_test_mode_actual_location, this, true)) {
			followGPS = false;
		} else {
			followGPS = true;
		}
		ActivityCompat.invalidateOptionsMenu(this);
	}

	protected void initUI() {
		super.initUI();
		initSidebar();
		showLegoFragment();
	}

	private void showLegoFragment() {
		
		mFragment = new LegoFragment();
		mFragment.setXML(PrefUtil.readString(getResources().getString(R.string.key_pref_xml), this, getResources().getString(R.string.default_xml)));
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Bundle args = new Bundle();
		args.putInt("perspective", Perspective.TYPE_3D_ABOVE);
		mFragment.setArguments(args);
		ft.replace(R.id.fragment_container, mFragment);
		ft.commit();
	}

	@Override
	public void onLocationChanged(Location location) {

		// if we don't care for location updates right now, just return immediately
		if (!followGPS) return;
		
		
		if (DEBUG) Log.d(TAG, "new location: " + location.getLatitude() + ", " + location.getLongitude());

		mLocation = location;
		mFragment.setLocation(mLocation);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case Constants.QUESTIONS_ACTIVITY_REQUEST_CODE:
			// if the survey wasn't finished, there won't be any data
			if (data == null) return;

			// if it was finished, remember the name of the object
			Bundle extras = data.getExtras();
			if (extras == null) return;
			String name = extras.getString("name");
			mFragment.setObjectName(name);

			break;
		case Constants.PREFERENCE_ACTIVITY_REQUEST_CODE:
			super.checkPreferences();
			mFragment.checkPreferences();
			break;
		}
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		super.onSensorChanged(event);

		if (useCompass) {
			mFragment.setOrientation(mOrientationZ);
		}
	}

	@Override
	public void onConnected(Bundle arg0) {
		super.onConnected(arg0);

		if (followGPS) {
			mLocation = mLocationClient.getLastLocation();
		}
		mFragment.setLocation(mLocation);
		updateLocationTextView();
	}

	@Override
	public void switchFragments(Fragment fragment) {
		// TODO Auto-generated method stub		
	}
}
